package org.agile4j.plugin.rest;

import java.util.ArrayList;
import java.util.List;

import org.agle4j.framework.helper.BeanHelper;
import org.apache.cxf.interceptor.Interceptor;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
import org.apache.cxf.jaxrs.provider.jsonp.JsonpInInterceptor;
import org.apache.cxf.jaxrs.provider.jsonp.JsonpPostStreamInterceptor;
import org.apache.cxf.jaxrs.provider.jsonp.JsonpPreStreamInterceptor;
import org.apache.cxf.message.Message;
import org.apache.cxf.rs.security.cors.CrossOriginResourceSharingFilter;

import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;

/**
 * REST 助手类
 * 
 * @author hanyx
 * @since 1.0.0
 */
public class RestHelper {

	private static final List<Object> providerList = new ArrayList<>() ;
	private static final List<Interceptor<? extends Message>> inInterecptorList = new ArrayList<>() ;
	private static final List<Interceptor<? extends Message>> outInterecptorList = new ArrayList<>() ;
	
	static {
		// 添加  JSON Provider
		JacksonJsonProvider jsonProvider = new JacksonJsonProvider() ;
		providerList.add(jsonProvider) ;
		
		// 添加  Logging Interceptor
		if (RestConfig.isLog()) {
			LoggingInInterceptor loggingInInterceptor = new LoggingInInterceptor() ;
			inInterecptorList.add(loggingInInterceptor) ;
			LoggingOutInterceptor loggingOutInterceptor = new LoggingOutInterceptor() ;
			inInterecptorList.add(loggingOutInterceptor) ;
		}
		
		// 添加  JSONP Interceptor
		if (RestConfig.isJsonp()) {
			JsonpInInterceptor jsonpInInterceptor = new JsonpInInterceptor() ;
			jsonpInInterceptor.setCallbackParam(RestConfig.getJsonpFunction());
			inInterecptorList.add(jsonpInInterceptor) ;
			JsonpPreStreamInterceptor jsonpPreStreamInterceptor = new JsonpPreStreamInterceptor() ;
			outInterecptorList.add(jsonpPreStreamInterceptor) ;
			JsonpPostStreamInterceptor jsonpPostStreamInterceptor = new JsonpPostStreamInterceptor() ;
			outInterecptorList.add(jsonpPostStreamInterceptor) ;
		}
		
		// 添加  CORS Provider
		if (RestConfig.isCors()) {
			CrossOriginResourceSharingFilter corsProvider = new CrossOriginResourceSharingFilter() ;
			corsProvider.setAllowOrigins(RestConfig.getCorsOriginList());
			providerList.add(providerList) ;
		}
	}
	
	/**
	 * 发布 REST 服务
	 * @param wadl
	 * @param resourceClass
	 */
	public static void publishService(String wadl, Class<?> resourceClass) {
		JAXRSServerFactoryBean factory = new JAXRSServerFactoryBean();
		factory.setAddress(wadl);
		factory.setResourceClasses(resourceClass);
		factory.setResourceProvider(resourceClass, new SingletonResourceProvider(BeanHelper.getBean(resourceClass)));
		factory.setProviders(providerList);
		factory.setInInterceptors(inInterecptorList);
		factory.setOutInterceptors(outInterecptorList);
		factory.create();
	}
	
	/**
	 * 创建 REST 客户端
	 * @param wadl
	 * @param resourceClass
	 * @return
	 */
	public static <T> T createClient(String wadl, Class<? extends T> resourceClass) {
		return JAXRSClientFactory.create(wadl, resourceClass, providerList);
	}
}
